/*
 * fakenect.h
 *
 *  Created on: 11-Oct-2011
 *      Author: yadvendar
 */
#include <libfreenect.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <omp.h>
#include <pthread.h>
#include <signal.h>
#include <arpa/inet.h>
#include <fcntl.h>

#include "ffmpegDec/ffmpeg.h"


#define MAXDATASIZE 1000000 // max number of bytes we can get at once

#define GRAVITY 9.80665
#define FRAMES_TO_DECODE (30)
#define TIMESTAMP_BYTES (10)
#define CUR_TIME_BYTES (20)
#define RGB_VIDEO_SIZE (300000)
#define DEPTH_VIDEO_SIZE (500000)
#define VIDEO_HEADER_BYTES (10)

// The dev and ctx are just faked with these numbers

static freenect_device *fake_dev = (freenect_device *)1234;
static freenect_context *fake_ctx = (freenect_context *)5678;
static freenect_depth_cb cur_depth_cb = NULL;
static freenect_video_cb cur_rgb_cb = NULL;
static freenect_raw_tilt_state state = {};
static int already_warned = 0;
static double playback_prev_time = 0.;
static double record_prev_time = 0.;
static void *depth_buffer = NULL;
static void *rgb_buffer = NULL;
static int depth_running = 0;
static int rgb_running = 0;
static void *user_ptr = NULL;
void *tmp_rgb=NULL;
void *tmp_depth=NULL;
struct sigaction sigact;



static int flag_back=0;

int thread_display_running=0,thread_decompress_running=0,thread_recv_running=0;
unsigned int array_timestamp_front[FRAMES_TO_DECODE];
unsigned int array_timestamp_back[FRAMES_TO_DECODE];
static unsigned int *array_timestamp=NULL;

double array_record_cur_time_front[FRAMES_TO_DECODE];
double array_record_cur_time_back[FRAMES_TO_DECODE];
static double *array_record_cur_time=NULL;

unsigned char buf[(TIMESTAMP_BYTES+CUR_TIME_BYTES)*FRAMES_TO_DECODE+VIDEO_HEADER_BYTES+RGB_VIDEO_SIZE+VIDEO_HEADER_BYTES+DEPTH_VIDEO_SIZE];
static unsigned char *rgbData=NULL;//[FRAMES_TO_DECODE* FREENECT_VIDEO_RGB_SIZE];
static unsigned char *depthData=NULL;//[FRAMES_TO_DECODE*(FREENECT_DEPTH_11BIT_SIZE/2)];
unsigned char temp_timestamp[TIMESTAMP_BYTES],temp_cur_time[CUR_TIME_BYTES];
int headerSize=(TIMESTAMP_BYTES+CUR_TIME_BYTES)*FRAMES_TO_DECODE+VIDEO_HEADER_BYTES;

int decompressed,received,displayed_prev,decompressed_prev;
unsigned char *outputRgb_front=NULL,*outputRgb_back=NULL,*outputDepth_front=NULL,*outputDepth_back=NULL;
unsigned char RED[FREENECT_VIDEO_RGB_SIZE];
int shmidRgb_front,shmidRgb_back,shmidDepth_front,shmidDepth_back;


pthread_mutex_t decompressed_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t decompressed_cond = PTHREAD_COND_INITIALIZER;

pthread_mutex_t recvd_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t recv_cond = PTHREAD_COND_INITIALIZER;

pthread_mutex_t displayed_prev_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t displayed_prev_cond = PTHREAD_COND_INITIALIZER;

pthread_mutex_t decompressed_prev_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t decompressed_prev_cond = PTHREAD_COND_INITIALIZER;

